package com.buybuyyi.stylistcrm.order.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.buybuyyi.stylistcrm.order.helper.OrderGoodsHelper;
import com.buybuyyi.stylistcrm.utils.DateUtil;
import com.buybuyyi.stylistcrm.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.buybuyyi.stylistcrm.order.dao.PurchaseOrderDao;
import com.buybuyyi.stylistcrm.order.mapper.PurchaseOrderMapper;
import com.buybuyyi.stylistcrm.order.po.PurchaseOrder;
import stylistcrm.core.services.order.dto.OrderByDto;
import stylistcrm.core.services.order.dto.OrderGoodsDto;
import stylistcrm.core.services.order.dto.PurchaseOrderDto;
import stylistcrm.core.services.order.dto.QueryOrderConditionDto;
import stylistcrm.core.services.order.enums.EnumOrderStatus;
import stylistcrm.core.services.order.enums.EnumOrderType;
import stylistcrm.core.services.order.enums.EnumPayWay;

@Repository
public class PurchaseOrderDaoImpl implements PurchaseOrderDao {

	@Autowired
	private PurchaseOrderMapper purchaseOrderMapper;
	
	@Override
	public int deleteByPrimaryKey(Integer id) {
		return purchaseOrderMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(PurchaseOrder record) {
		return purchaseOrderMapper.insert(record);
	}

	@Override
	public int insertSelective(PurchaseOrder record) {
		return purchaseOrderMapper.insertSelective(record);
	}

	@Override
	public PurchaseOrder selectByPrimaryKey(Integer id) {
		return purchaseOrderMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(PurchaseOrder record) {
		return purchaseOrderMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(PurchaseOrder record) {
		return purchaseOrderMapper.updateByPrimaryKey(record);
	}

	@Override
	public List<PurchaseOrderDto> getPageList(QueryOrderConditionDto queryCondition) {
		OrderGoodsHelper.setDefaultOrderBy(queryCondition, EnumOrderType.PURCHASE_ORDER);
		List<PurchaseOrderDto> purchaseOrderDtoList=purchaseOrderMapper.getPageList(queryCondition);
		if(null!=purchaseOrderDtoList&&0!=purchaseOrderDtoList.size()){
			for(PurchaseOrderDto purchaseOrder:purchaseOrderDtoList){

				List<OrderGoodsDto> orderGoodsDtoList=purchaseOrder.getGoods();
				OrderGoodsHelper.setGoodsNameAndCount(orderGoodsDtoList,purchaseOrder);
				if(StringUtil.isNotBlank(purchaseOrder.getOrderStatus())){
					purchaseOrder.setOrderStatus(EnumOrderStatus.getEnum(purchaseOrder.getOrderStatus()).getDesc());
				}
				if(StringUtil.isNotBlank(purchaseOrder.getPayWay())){
					purchaseOrder.setPayWay(EnumPayWay.getEnum(purchaseOrder.getPayWay()).getDesc());
				}
				if(StringUtil.isNotBlank(purchaseOrder.getPayTime())){
					purchaseOrder.setPayTime(DateUtil.formatDatetime(purchaseOrder.getPayTime()));
				}
				if(StringUtil.isNotBlank(purchaseOrder.getOrderTime())){
					purchaseOrder.setOrderTime(DateUtil.formatDatetime(purchaseOrder.getOrderTime()));
				}
			}
		}
		return purchaseOrderDtoList;
	}

	@Override
	public List<Integer> getPageIdList(QueryOrderConditionDto queryCondition) {
		return purchaseOrderMapper.getPageIdList(queryCondition);
	}

	@Override
	public Integer getTotalCount(QueryOrderConditionDto queryCondition) {
		return purchaseOrderMapper.getTotalCount(queryCondition);
	}
}
